package com.gitee.wsl.data.recycler

import com.gitee.wsl.data.recycler.RecyclerPool.WithPool


/*
/ **********************************************************************
/ * Partial/base RecyclerPool implementations
/ **********************************************************************
 */
/**
 * Default [RecyclerPool] implementation that uses
 * [ThreadLocal] for recycling instances.
 * Instances are stored using [java.lang.ref.SoftReference]s so that
 * they may be Garbage Collected as needed by JVM.
 *
 *
 * Note that this implementation may not work well on platforms where
 * [java.lang.ref.SoftReference]s are not well supported (like
 * Android), or on platforms where [java.lang.Thread]s are not
 * long-living or reused (like Project Loom).
 */
abstract class ThreadLocalPoolBase<P : WithPool<P>> protected constructor() :
    RecyclerPool<P> {
    // // // Actual API implementation
    override fun acquireAndLinkPooled(): P {
        // since this pool doesn't do anything on release it doesn't need to be registered on the BufferRecycler
        return acquirePooled()
    }

    abstract override fun acquirePooled(): P

    override fun releasePooled(pooled: P) {
        // nothing to do, relies on ThreadLocal
    }

    // No way to actually even estimate...
    override fun pooledCount(): Int {
        return -1
    }

    // Due to use of ThreadLocal no tracking available; cannot clear
    override fun clear(): Boolean {
        return false
    }

}
